package com.cinemahorairerapido;

import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.cinemahorairerapido.adapter.FavoriteCinemaAdapter;
import com.cinemahorairerapido.bean.Cinema;
import com.cinemahorairerapido.controller.FavorisController;
import com.cinemahorairerapido.controller.FindCinemaProgramTask;

public class CinemaHoraireRapidoActivity extends ListActivity {

	private static final int MENU_HELP = Menu.FIRST;

	private static final int MENU_ABOUT = Menu.FIRST + 1;

	private static final int DIALOG_ABOUT = 0;

	private static final int DIALOG_HELP = 1;

	private TextView noFavoriteTextView = null;

	private ProgressDialog progressdialog;

	private FindCinemaProgramTask task = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// block rotation
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

		Button findCinemaButton = (Button) findViewById(R.id.findCinema);

		findCinemaButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(CinemaHoraireRapidoActivity.this, FindCinemaActivity.class);
				CinemaHoraireRapidoActivity.this.startActivity(i);
			}
		});

		noFavoriteTextView = (TextView) findViewById(R.id.emptyfavoritetextview);

	}

	@Override
	protected void onResume() {
		super.onResume();

		List<Cinema> favoriteCinemas = FavorisController.getInstance(this).getFavoriteCinemas();
		this.setListAdapter(new FavoriteCinemaAdapter(favoriteCinemas, this));

		if (favoriteCinemas == null || favoriteCinemas.isEmpty()) {
			noFavoriteTextView.setText(getResources().getString(R.string.main1));
		} else {
			noFavoriteTextView.setText("");
		}
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);

		Cinema cinema = null;
		List<Cinema> favoriteCinemas = FavorisController.getInstance(this).getFavoriteCinemas();

		for (int i = 0; i < favoriteCinemas.size() && cinema == null; i++) {
			if (favoriteCinemas.get(i).getId() != null && favoriteCinemas.get(i).getId() == (int) id) {
				cinema = favoriteCinemas.get(i);
			}
		}

		// Find data before launching view
		progressdialog = ProgressDialog.show(this, "", getResources().getText(R.string.getshowtime), true, true,
				new DialogInterface.OnCancelListener() {

					@Override
					public void onCancel(DialogInterface dialog) {
						if (task != null) {
							task.cancel(true);
						}
					}
				});

		task = new FindCinemaProgramTask(this, progressdialog);
		task.execute(cinema);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, MENU_HELP, 0, R.string.help);
		menu.add(0, MENU_ABOUT, 1, R.string.about);
		MenuItem item = menu.getItem(0);
		item.setIcon(android.R.drawable.ic_menu_help);
		item = menu.getItem(1);
		item.setIcon(android.R.drawable.ic_menu_info_details);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {

		case MENU_ABOUT:
			showDialog(DIALOG_ABOUT);
			break;
		case MENU_HELP:
			showDialog(DIALOG_HELP);
			break;
		}

		return super.onMenuItemSelected(featureId, item);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		Dialog dialog;
		switch (id) {
		case DIALOG_ABOUT:
			dialog = createAboutDialog();
			break;
		case DIALOG_HELP:
			dialog = createHelpDialog();
			break;
		case FindCinemaProgramTask.DIALOG_PARSE_KO:
			dialog = FindCinemaProgramTask.createJasonParseErrorDialog(this);
			break;
		case FindCinemaProgramTask.DIALOG_TIMEOUT:
			dialog = FindCinemaProgramTask.createNoConnectionDialog(this);
			break;
		default:
			dialog = null;
		}
		return dialog;
	}

	public AlertDialog createHelpDialog() {
		final TextView message = new TextView(this);
		message.setPadding(10, 10, 10, 10);

		String s = this.getString(R.string.help1);

		message.setText(s);
		message.setMovementMethod(LinkMovementMethod.getInstance());

		return new AlertDialog.Builder(this).setTitle(R.string.help)//
				.setCancelable(true)//
				.setIcon(android.R.drawable.ic_dialog_info)//
				.setView(message).create();
	}

	public AlertDialog createAboutDialog() {
		final TextView message = new TextView(this);
		message.setPadding(10, 10, 10, 10);

		final SpannableString s = new SpannableString(this.getText(R.string.about1));
		Linkify.addLinks(s, Linkify.WEB_URLS);
		message.setText(s);
		message.setMovementMethod(LinkMovementMethod.getInstance());

		return new AlertDialog.Builder(this).setTitle(R.string.about)//
				.setCancelable(true)//
				.setIcon(android.R.drawable.ic_dialog_info)//
				.setView(message).create();
	}

}